package service;

import mapper.BookMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Book;
import tools.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/4.
 */
@Service("bookService")
public class BookServiceImpl implements BookService {
    @Resource
    BookMapper bookMapper;

    public Page bookList(Integer id, String name, Page pageObj) {
        // 查询总条数
        int totalCount = bookMapper.getCount(id, name);
        // 设置总数、总页数
        pageObj.setTotalCount(totalCount);
        // 如果总条数>0,说明数据库中有数据，再查询具体数据
        if (totalCount > 0) {
            // （用户一直点击下一页）如果当前页大于总页数，把总页数的值赋给当前页的值，显示最后一页的值
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Book> List = bookMapper.getBookList(id, name, (pageObj.getCurrPageNo() - 1) * pageObj.getPageSize(), pageObj.getPageSize());

            pageObj.setList(List);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setList(new ArrayList<Book>());
        }
        return pageObj;

    }

    @Override
    public int add(Book book) {
        return bookMapper.add(book);
    }

    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }

    public Book selectBookId(int bookId){
        return bookMapper.selectBookId(bookId);
    }

    public int deleteById(int bookId){
        return bookMapper.deleteById(bookId);
    }

}
